package com.pansky.crm.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pansky.crm.common.mybatisplus.ApiResult;
import com.pansky.crm.common.mybatisplus.SuperServiceImpl;
import com.pansky.crm.entity.Privilege;
import com.pansky.crm.entity.vo.RolePrivilegeTreeVO;
import com.pansky.crm.entity.vo.RolePrivilegeVo;
import com.pansky.crm.enums.PrivilegeTypeEnum;
import com.pansky.crm.mapper.mysql.PrivilegeMapper;
import com.pansky.crm.service.PrivilegeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：zhouzhiyong
 * @date ：Created in 2021/2/22 10:37
 * @description：权限服务
 */
@Service
@DS("mysqlDb")
@Slf4j
public class PrivilegeServiceImpl extends SuperServiceImpl<PrivilegeMapper, Privilege> implements PrivilegeService {

    @Autowired
    private PrivilegeMapper privilegeMapper;
    @Override
    public List<Privilege> findPrivilegeByRoleId(Integer roleId) {
        return privilegeMapper.findPrivilegeByRoleId(roleId);
    }

    @Override
    public RolePrivilegeTreeVO findMenuPrivilegeByRoleId(Integer roleId) {
        RolePrivilegeTreeVO rolePrivilegeTreeVO = new RolePrivilegeTreeVO();
        List<RolePrivilegeVo> rolePrivilegeVos = privilegeMapper.findMenuPrivilege(null);
        List<Privilege> selectPrivileges = privilegeMapper.findPrivilegeByRoleId(roleId);
        List<String> privilegeKeys = selectPrivileges.stream().map(Privilege::getKey).collect(Collectors.toList());
        rolePrivilegeTreeVO.setRoleId(roleId);
        rolePrivilegeTreeVO.setPrivilege(rolePrivilegeVos);
        rolePrivilegeTreeVO.setSelectedKey(privilegeKeys);
        return rolePrivilegeTreeVO;
    }

    @Override
    public List<RolePrivilegeVo> findMenuPrivilege() {
        List<RolePrivilegeVo> rolePrivilegeVos = privilegeMapper.findMenuPrivilege(null);
        getRolePrivilegeVos(rolePrivilegeVos);
        return rolePrivilegeVos;
    }

    @Override
    public ApiResult<Boolean> removeByPrivilegeId(Integer privilegeId) {
        Privilege privilege = getById(privilegeId);
        QueryWrapper<Privilege> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_key", privilege.getKey());
        List<Privilege> privilegeList = list(queryWrapper);
        if (CollectionUtils.isNotEmpty(privilegeList)) {
            return ApiResult.failed("当前菜单下有子菜单，无法删除！");
        }
        return ApiResult.ok(removeById(privilegeId));
    }

    private void getRolePrivilegeVos(List<RolePrivilegeVo> rolePrivilegeVos) {
        if (null != rolePrivilegeVos) {
            Iterator<RolePrivilegeVo> iterator = rolePrivilegeVos.listIterator();
            while (iterator.hasNext()) {
                RolePrivilegeVo next = iterator.next();
                if (next.getType().equals(PrivilegeTypeEnum.POINTS.getValue())) {
                    iterator.remove();
                }
                getRolePrivilegeVos(next.getChildren());
            }
        }
    }

}
